﻿using MyEartags.EarTag;
using MyEartags.Enumerate;
using MyEartags.SheepTag;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace MyEartags.WearSign
{
    public class SheepLabelAppService : ApplicationService, ISheepLabelAppService
    {
        private readonly IRepository<SheepLabel, int> _sheepLabelRepository;

        private readonly IRepository<Earoverbit, int> _earoverbitRepository;

        public SheepLabelAppService(IRepository<SheepLabel, int> sheepLabelRepository, IRepository<Earoverbit, int> earoverbitRepository)
        {
            _sheepLabelRepository = sheepLabelRepository;
            _earoverbitRepository = earoverbitRepository;
        }

        /// <summary>
        /// 获取戴标原因列表
        /// </summary>
        /// <returns></returns>
        public List<object> GetLabelCauseInfoList()
        {
            List<object> list = new List<object>();

            foreach (var item in Enum.GetValues<LabelCause>())
            {
                list.Add(new
                {
                    cname = item.ToString(),
                    cId = item
                });
            }

            return list.ToList();
        }

        /// <summary>
        /// 获取标签状态列表
        /// </summary>
        /// <returns></returns>
        public List<object> GetLabelStateInfoList()
        {
            List<object> list = new List<object>();

            foreach (var item in Enum.GetValues<LabelState>())
            {
                list.Add(new
                {
                    sname = item.ToString(),
                    sId = item
                });
            }

            return list.ToList();
        }

        /// <summary>
        /// 羊只戴标新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<SignResult<SheepLabelDto>> CreateSheepLabel(SheepLabelDto dto)
        {
            SheepLabel sheepLabel = new SheepLabel();

            sheepLabel.Overid = dto.Overid;
            sheepLabel.ElectronicEar = dto.ElectronicEar;
            sheepLabel.NewElectronicEar = dto.NewElectronicEar;
            sheepLabel.LabelCause = (LabelCause)dto.LabelCause;
            sheepLabel.LabelState = (LabelState)dto.LabelState;
            sheepLabel.AddPeople = dto.AddPeople;
            sheepLabel.UserTime = dto.UserTime;

            var result = await _sheepLabelRepository.InsertAsync(sheepLabel);
            var res = ObjectMapper.Map<SheepLabel, SheepLabelDto>(result);

            if (res != null)
            {
                return new SignResult<SheepLabelDto>() { Code = 200, Message = "羊只戴标新增成功！" };
            }
            else
            {
                return new SignResult<SheepLabelDto>() { Code = 500, Message = "羊只戴标新增失败！" };
            }
        }

        /// <summary>
        /// 羊只戴标列表
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<SheepLabelShowDto>> GetSheeplabelList(string? overbitName, string? electronicEar, string? newElectronicEar)
        {
            var list = (from a in (await _sheepLabelRepository.GetListAsync())
                        join b in (await _earoverbitRepository.GetListAsync()) on a.Overid equals b.Id
                        select new SheepLabelShowDto
                        {
                            Id = a.Id,
                            Overid = a.Overid,
                            ElectronicEar = a.ElectronicEar,
                            NewElectronicEar = a.NewElectronicEar,
                            LabelCause = (int)a.LabelCause,
                            LabelState = (int)a.LabelState,
                            AddPeople = a.AddPeople,
                            UserTime = a.UserTime,
                            OverbitName = b.OverbitName,

                        }).AsQueryable();

            if (!string.IsNullOrEmpty(overbitName))
            {
                list = list.Where(a => a.OverbitName.Contains(overbitName));
            }

            if (!string.IsNullOrEmpty(electronicEar))
            {
                list = list.Where(a => a.ElectronicEar.Contains(electronicEar));
            }

            if (!string.IsNullOrEmpty(newElectronicEar))
            {
                list = list.Where(a => a.NewElectronicEar.Contains(newElectronicEar));
            }

            return list;
        }

        public async Task DeleteAsync(int sid)
        {
            await _sheepLabelRepository.DeleteAsync(sid);
        }
    }
}
